package com.seagull.sb.service;

import java.util.Collections;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.seagull.sb.bo.OrganizationBO;
import com.seagull.sb.domain.Division;
import com.seagull.sb.domain.Organization;
import com.seagull.sb.domain.Subject;
import com.seagull.sb.domain.Teacher;
import com.seagull.sb.dto.TeacherDto;
import com.seagull.sb.util.SBRequestContext;
import com.seagull.sb.util.ServiceMapper;

@Component
@Path("/organization")
public class OrganizationService extends BaseService {

	@Autowired
	OrganizationBO organizationBO;
	
	private ServiceMapper sm = ServiceMapper.getInstaMapper();
	
	@GET
	@Path("/")
	@Produces(MediaType.APPLICATION_JSON)
	@RolesAllowed({"teacher","parent"})
	public Organization getOrganization(@Context SecurityContext sc) {
		SBRequestContext rc = populateRequestContext(sc); 

		try {
			return organizationBO.getOrganization(rc);
			//return sm.mapToTeacherDto(teachers);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new Organization();
	}
	
	@GET
	@Path("/teachers")
	@Produces(MediaType.APPLICATION_JSON)
	@RolesAllowed({"teacher"})
	public List<TeacherDto> getTeachers(@Context SecurityContext sc) {
		SBRequestContext rc = populateRequestContext(sc); 

		List<Teacher> teachers;
		try {
			teachers = organizationBO.getTeachers(rc);
			return sm.mapToTeacherDto(teachers);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}
	
	@GET
	@Path("/divisions")
	@Produces(MediaType.APPLICATION_JSON)
	@RolesAllowed({"teacher"})
	public List<Division> getDivisions(@Context SecurityContext sc) {
		SBRequestContext rc = populateRequestContext(sc); 

		List<Division> divisions;
		try {
			divisions = organizationBO.getDivisions(rc);
			return divisions;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}	
	
	@GET
	@Path("/subjects")
	@Produces(MediaType.APPLICATION_JSON)
	@RolesAllowed({"teacher"})
	public List<Subject> getSubjects(@Context SecurityContext sc) {
		SBRequestContext rc = populateRequestContext(sc); 

		List<Subject> subjects;
		try {
			subjects = organizationBO.getSubjects(rc);
			return subjects;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}	
	
}